<?php
/**
 * Created by PhpStorm.
 * User: *!N.J
 * Date: 2021/8/22
 * Time: 4:26
 * QQ:1467572213
 */

namespace app\ch\controller;

use think\facade\Request;
use think\facade\Session;
use \app\ch\model\Withdrawal as Withdrawal_m;

class Withdrawal extends Base
{
    public function index()
    {
        $user_id = Session::get('user_id');
        $user_info = \app\ch\model\Base::get_user_info($user_id);

        $service_tx = \app\ch\model\Base::get_config($name = 'service_tx');
        $this->assign('service_tx',$service_tx);

        $min_money = \app\ch\model\Base::get_config($name = 'min_money');
        $this->assign('min_money',$min_money);

        if (!$user_info['bank_name'] || !$user_info['holder_name'] || !$user_info['account_number'] || !$user_info['phone_number']){
            $is_card = 0;
        }else{
            $is_card = 1;
        }
        $this->assign('is_card',$is_card);
        return $this->fetch();
    }

    public function confirm_withdrawal()
    {
        $user_id = Session::get('user_id');
        $user_info = \app\ch\model\Base::get_user_info($user_id);
        $start_withdrawal = \app\ch\model\Base::get_config($name = 'start_withdrawal');
        $service_charge = \app\ch\model\Base::get_config($name = 'service_tx');
        $min_money = \app\ch\model\Base::get_config($name = 'min_money');
        if (Request::isPost()){
            $money = Request::post('money');
            $pass = Request::post('pass');
            $check_pass = Withdrawal_m::check_pass($user_id,$pass);
            if ($check_pass == 'error') return ['code'=>0,'msg'=>'Transaction password error'];
            $check_order = Withdrawal_m::check_order($user_id);
            if ($check_order == 'error') return ['code'=>0,'msg'=>'The task has not been completed. Please submit the application after completing the task'];
            $check_group_order_num = Withdrawal_m::check_group_order_num($user_info);
            if ($check_group_order_num == 'error') return ['code'=>0,'msg'=>'There are still unfinished orders in the group'];
            if (!$user_info['bank_name'] || !$user_info['holder_name'] || !$user_info['account_number'] || !$user_info['phone_number'] || !$user_info['ifsc'] || !$user_info['upi']) return ['code'=>0,'msg'=>'Please bind your bank card before you make a withdrawal'];

            if ($money < $start_withdrawal) return ['code'=>0,'msg'=>'Less than the starting amount'];
            if ($money > $user_info['balance']) return ['code'=>0,'msg'=>'Your balance is insufficient'];
            $confirm = Withdrawal_m::confirm_withdrawal($user_info,$money,$service_charge,$min_money);
            switch ($confirm)
            {
                case 'success':
                    return ['code'=>1,'msg'=>'Success'];
                    break;
                case 'error':
                    return ['code'=>0,'msg'=>'Error'];
                    break;
            }
        }
    }
}